#include <bits/stdc++.h>

#define pb push_back
#define eb emplace_back
#define fi first
#define se second
#define in read<int>()
#define lin read<ll>()
#define rep(i, x, y) for(int i = (x); i <= (y); i++)
#define per(i, x, y) for(int i = (x); i >= (y); i--)

using namespace std;

using ll = unsigned long long;
using db = double;
using pii = pair < int, int >;
using vec = vector < int >;
using veg = vector < pii >;

template < typename T > T read() {
	T x = 0; bool f = 0; char ch = getchar();
	while(!isdigit(ch)) f |= ch == '-', ch = getchar();
	while(isdigit(ch)) x = x * 10 + (ch ^ 48), ch = getchar();
	return f ? -x : x;
}

template < typename T > void chkmax(T &x, const T &y) { x = x > y ? x : y; }
template < typename T > void chkmin(T &x, const T &y) { x = x < y ? x : y; }

int id[2620], trans[50][10], ind[50];
bool vis[2620];
ll l, r;
int pot[20], top;
ll f[20][2][2620][50];

ll dfs(ll pos, int fl, int res, int stu) {
	if(pos == 0) return res % ind[stu] == 0 ? 1 : 0;
	if(~f[pos][fl][res][stu]) return f[pos][fl][res][stu];
	ll tot = 0;
	int up = fl ? pot[pos] : 9;
	rep(i, 0, up) tot += dfs(pos - 1, fl & (i == pot[pos]), (res * 10 + i) % 2520, trans[stu][i]);
	return f[pos][fl][res][stu] = tot;
}

ll calc(ll x) {
	top = 0; while(x > 0) pot[++top] = x % 10, x /= 10;
	memset(f, -1, sizeof f); return dfs(top, 1, 0, id[1]);
}

int main() {
#ifndef ONLINE_JUDGE
	freopen("1.in", "r", stdin);
#endif
	int cnt = 0;
	rep(i, 0, 1 << 9) {
		int lcm = 1;
		rep(j, 0, 8) if(i >> j & 1) lcm = lcm / __gcd(lcm, j + 1) * (j + 1);
		if(!id[lcm]) ind[id[lcm] = ++cnt] = lcm;
	} 
	rep(i, 1, cnt) rep(j, 0, 9) trans[i][j] = id[j == 0 ? ind[i] : ind[i] / __gcd(ind[i], j) * j];
	int t; cin >> t; while(t--) cin >> l >> r, cout << calc(r) - calc(l - 1) << endl;
	return 0;
}
